
#ifndef _SMART_VOICE__VOICERECO_SESSION_H__INCLUDED_
#define _SMART_VOICE__VOICERECO_SESSION_H__INCLUDED_

#include <map>

#include "Nebula.h"

namespace smartvoice {

using namespace nebula::sdk;

class VoiceRecoSession;

class VoiceSessionManager
{
	typedef std::map<std::string, std::string> str2str_map;
	typedef std::map<std::string, str2str_map> nested_str2str_map;

public:
	VoiceSessionManager();
	~VoiceSessionManager();

public:
	NEBULA_ERROR_CODE InitEnv();
	NEBULA_ERROR_CODE LoadConfig(const char* config_path);

	NEBULA_ERROR_CODE SetConfig(const char* key, const char* value);
	NEBULA_ERROR_CODE DelConfig(const char* key);

	NEBULA_ERROR_CODE SetSessionTemplate(const char* name, const char* field, const char* val);
	NEBULA_ERROR_CODE DelSessionTemplate(const char* name);

public:
	VoiceRecoSession* AllocSession(const char* templatename);
	void              CloseSession(VoiceRecoSession* psession);

public:
	static std::string ComposeConfStr(str2str_map& keypairs);

private:
	str2str_map        _config_map;
	nested_str2str_map _template_map;
	nested_str2str_map _gramma_map;
};

class VoiceRecoSession
{
public:
	VoiceRecoSession(
		const long fragsize, 
		const std::string& conf, 
		const std::string& grammar
		);
	~VoiceRecoSession();

public:	
	NEBULA_ERROR_CODE CommitTask(ITask* ptask);
	const char* GetSessionID() const;

private:
	NEBULA_ERROR_CODE BeginSession();
	NEBULA_ERROR_CODE EndSession  ();
	NEBULA_ERROR_CODE WriteData   (const void* data, int size);
	NEBULA_ERROR_CODE GetResult   (std::string& output);
	int WriteAudio(const char* data, int size, int mode);

private:
	const char* _session_id;
	const long  _fragment_size;
	const std::string _config;
	const std::string _grammar;
	
};

} // namespace smartvoice

#endif // _SMART_VOICE__VOICERECO_SESSION_H__INCLUDED_

